package cn.nexd.location.collector;

import android.content.Context;

import java.util.List;

import cn.nexd.location.NexdLocationConfig;
import cn.nexd.location.collector.bean.BluetoothCollectorResult;
import cn.nexd.location.collector.bean.WifiCollectorResult;
import cn.nexd.location.collector.core.Collector;
import cn.nexd.location.collector.core.CollectorWrap;
import cn.nexd.location.collector.core.collectors.CollectorFactory;
import cn.nexd.location.exception.ContextNullException;

/**
 * 采集器调度。在这里负责生产采集器，统一开启。
 * Created by codingfish on 15/10/10.
 */
final class CollectorDispatcher {

    private Context context;

    private NexdCollectorConfig config;
    private Collector.WifiCollectorListener wifiCollectorListener;

    private CollectorWrap wifiCollectorWrap;
    private Collector.BluetoothCollectorListener bluetoothCollectorListener;
    private CollectorWrap bluetoothCollectorWrap;

    /**
     * 设置上下文对象。强烈建议在初始化之后立刻调用
     *
     * @param context 当前上下文对象
     */
    public void setContext(Context context) {
        this.context = context;
    }

    /**
     * 设置 Collector 的配置对象。
     *
     * @param config
     * @see NexdLocationConfig
     */
    public void setCollectorConfig(NexdCollectorConfig config) {
        this.config = config;
    }


    /**
     * 设置 Wifi 采集器监听。
     *
     * @param wifiCollectorListener
     */
    public void setWifiCollectorListener(Collector.WifiCollectorListener wifiCollectorListener) {
        this.wifiCollectorListener = wifiCollectorListener;
    }

    /**
     * 设置蓝牙采集器监听
     *
     * @param bluetoothCollectorListener
     */
    public void setBluetoothCollectorListener(Collector.BluetoothCollectorListener bluetoothCollectorListener) {
        this.bluetoothCollectorListener = bluetoothCollectorListener;
    }

    /**
     * 开启采集器。
     */
    public void startCollector() {
        //校验 Context 是否为 null
        this.checkContext(context);
        this.configCollector(config);

        if (wifiCollectorWrap != null) {
            wifiCollectorWrap.startCollector();
        }

        if (bluetoothCollectorWrap != null) {
            bluetoothCollectorWrap.startCollector();
        }

    }

    /**
     * 停止采集器。
     */
    public void stopCollector() {
        if (wifiCollectorWrap != null) {
            wifiCollectorWrap.stopCollector();
        }

        if (bluetoothCollectorWrap != null) {
            bluetoothCollectorWrap.stopCollector();
        }
    }

    public void destroyCollector() {
        if (context != null) {
            context = null;
        }

        if (config != null) {
            config = null;
        }
        if (wifiCollectorListener != null) {
            wifiCollectorListener = null;
        }

        if (wifiCollectorWrap != null) {
            wifiCollectorWrap.destroyCollector();
            wifiCollectorWrap = null;
        }

        if (bluetoothCollectorListener != null) {
            bluetoothCollectorListener = null;
        }

        if (bluetoothCollectorWrap != null) {
            bluetoothCollectorWrap.destroyCollector();
            bluetoothCollectorWrap = null;
        }
    }

    //校验 Context 对象
    private void checkContext(Context context) {
        if (context == null) {
            throw new ContextNullException();
        }
    }

    //根据 Config，生成采集器对象
    private void configCollector(NexdCollectorConfig collectorConfig) {
        if (collectorConfig == null) {
            //默认配置
            collectorConfig = new NexdCollectorConfig.Buidler().build();
        }

        // Wifi
        if (collectorConfig.wifiCollectorEnable) {
            wifiCollectorWrap = new CollectorWrap(CollectorFactory.getWifiCollector(context, collectorConfig, new WifiCollectorListener()));
        }

        //蓝牙
        if (collectorConfig.bluetoothCollectorEnable) {
            bluetoothCollectorWrap = new CollectorWrap(CollectorFactory.getBluetoothCollector(context, collectorConfig, new BluetoothCollectorListener()));
        }

    }

    private class WifiCollectorListener implements Collector.WifiCollectorListener {

        @Override
        public void onFailed(long collectorResultTaskId, long collectorResultTaskTime, int collectorResultTaskFailedType, String collectorResultTaskFailedMessage) {
            if (wifiCollectorListener != null) {
                wifiCollectorListener.onFailed(collectorResultTaskId, collectorResultTaskTime, collectorResultTaskFailedType, collectorResultTaskFailedMessage);
            }
        }

        @Override
        public void onSuccess(long collectorResultTaskId, long collectorResultTaskTime, List<WifiCollectorResult> wifiCollectorResults) {
            if (wifiCollectorListener != null) {
                wifiCollectorListener.onSuccess(collectorResultTaskId, collectorResultTaskTime, wifiCollectorResults);
            }
        }

        @Override
        public void onStop(long collectorResultTaskId, long collectorResultTaskTime, int collectorResultTaskStopType, String collectorResultTaskStopMessage) {
            if (wifiCollectorListener != null) {
                wifiCollectorListener.onStop(collectorResultTaskId, collectorResultTaskTime, collectorResultTaskStopType, collectorResultTaskStopMessage);
            }
        }
    }

    private class BluetoothCollectorListener implements Collector.BluetoothCollectorListener {

        @Override
        public void onFailed(long collectorResultTaskId, long collectorResultTaskTime, int collectorResultTaskFailedType, String collectorResultTaskFailedMessage) {
            if (bluetoothCollectorListener != null) {
                bluetoothCollectorListener.onFailed(collectorResultTaskId, collectorResultTaskTime, collectorResultTaskFailedType, collectorResultTaskFailedMessage);
            }
        }

        @Override
        public void onStop(long collectorResultTaskId, long collectorResultTaskTime, int collectorResultTaskStopType, String collectorResultTaskStopMessage) {
            if (bluetoothCollectorListener != null) {
                bluetoothCollectorListener.onStop(collectorResultTaskId, collectorResultTaskTime, collectorResultTaskStopType, collectorResultTaskStopMessage);
            }
        }

        @Override
        public void onSuccess(long collectorResultTaskId, long collectorResultTaskTime, List<BluetoothCollectorResult> bluetoothCollectorResults) {
            if (bluetoothCollectorListener != null) {
                bluetoothCollectorListener.onSuccess(collectorResultTaskId, collectorResultTaskTime, bluetoothCollectorResults);
            }
        }
    }
}
